from event_rule.dwc import NO_BET_AMOUNT_GAME_TYPE, NO_WIN_AMOUNT_GAME_TYPE

USER_ANALYSIS_CONFIG = {
    'real_time_report': False,
    'group_by': '_chn.keyword',
    'interval_type_list': ['day', 'hour'],
    'dynamic_argument': True,
    'target_event_id': ['bet_order', 'recharge', 'withdraw', 'register', 'active', 'bind', 'recharge_apply'],
    'values': [
        {
            "value_id": "new_device",
            "target_value": "_aid.keyword",
            "value_name": "新增设备",
            "filter": [
                {'term': {'_event_id': 'active'}},
                {'term': {"_is_new_device": 1}}
            ],
            "operation": "cardinality",
            "value_type": "long",
            "description": "当日新增的设备数量"
        },
        {
            "value_id": "active_device",
            "target_value": "_aid.keyword",
            "value_name": "登录设备",
            "filter": [
                {'term': {'_event_id': 'active'}},
            ],
            "operation": "cardinality",
            "value_type": "long",
            "description": "当日登录大厅的设备数量"
        },
        {
            "value_id": "new_active_user",
            "target_value": "_user_id",
            "value_name": "新增账号",
            "filter": [
                {'term': {'_event_id': 'register'}},
            ],
            "operation": "value_count",
            "value_type": "long",
            "description": "当日新增的账号数量"
        },
        {
            "value_id": "active_user",
            "target_value": "_user_id",
            "value_name": "登录账号",
            "filter": [
                {'term': {'_event_id': 'active'}}
            ],
            "not_filter": [
                {'term': {'_user_id': 1}}
            ],
            "operation": "cardinality",
            "value_type": "long",
            "description": "当日登录大厅的用户数量"
        },
        {
            "value_id": "register_user",
            "target_value": "_user_id",
            "value_name": "新增绑定",
            "filter": [
                {'term': {'_event_id': 'bind'}}
            ],
            "not_filter": [
                {'term': {'_user_id': 1}}
            ],
            "operation": "cardinality",
            "value_type": "long",
            "description": "当日进行手机号绑定的用户数量"
        },
        {
            "value_id": "register_rate",
            "value_name": "绑定率",
            "value_expr": "register_user/(real_active_user-active_bind_user+register_user)",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100)),
            "description": "绑定账号 / 登录账号"
        },
        {
            "value_id": "real_active_user",
            "target_value": "_user_id",
            "value_name": "活跃帐号",
            "filter": [
                {'terms': {'_event_id': ['bet_order', 'active', 'register', 'recharge', 'withdraw']}},
            ],
            "not_filter": [
                {'term': {"bet_order_user_type": 1}}
            ],
            "value_type": "long",
            "operation": "cardinality",
            "description": "当日具任一活跃行为的用户数量，\n包含登录、注册、充值、提现、投注等行为"
        },
        {
            "value_id": "active_bind_user",
            "target_value": "_user_id",
            "value_name": "活跃绑定",
            "filter": [
                {"terms": {"_user_id": "${active_bind_user_pool}"}}
            ],
            "value_type": "long",
            "operation": "cardinality",
            "description": "当日活跃账号中绑定手机号的账号数量"
        },
        {
            "value_id": "bet_user",
            "target_value": "_user_id",
            "value_name": "投注账号",
            "filter": [
                {'terms': {'_event_id': ['bet_order']}},
            ],
            "not_filter": [
                {'term': {"bet_order_user_type": 1}}
            ],
            "value_type": "long",
            "operation": "cardinality",
            "description": "当日具下注行为的用户数量"
        },
        {
            "value_id": "bet_order_bind_user",
            "target_value": "_user_id",
            "value_name": "投注绑定",
            "filter": [
                {"terms": {"_user_id": "${bet_order_bind_user_pool}"}}
            ],
            "value_type": "long",
            "operation": "cardinality",
            "description": "当日投注用户中绑定手机号的账号数量"
        },
        {
            "value_id": "active_rate",
            "value_name": "投注率",
            "value_expr": "bet_user/active_user",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100)),
            "description": "投注账号 / 活跃账号"
        },
        {
            "value_id": "_bet_amount",
            "target_value": ["bet_order_bet_amount"],
            "value_name": "投注金额",
            "filter": [
                {'terms': {'_event_id': ['bet_order']}}
            ],
            "value_type": "float",
            "adjust_amount": True,
            "hide_result": True,
            "description": "游戏投注金额总和"
        },
        {
            "value_id": "bet_amount",
            "value_name": "投注金额",
            "value_expr": "_bet_amount - lose_award_amount",
            "adjust_amount": True,
            "description": "游戏投注金额总和"
        },
        {
            "value_id": "tax_amount",
            "target_value": "bet_order_tax_amount",
            "value_name": "玩法税收",
            "filter": [
                {'terms': {'_event_id': ['bet_order']}},
                {'term': {"bet_order_user_type": 0}}
            ],
            "value_type": "float",
            "adjust_amount": True,
            "description": "游戏中赢家所产生的固定比例手续费"
        },
        {
            "value_id": "high_level_fish_bet_amount",
            "target_value": 'bet_order_bet_amount',
            "target_index": ["dark3", 'dark2', 'dark4'],
            "value_name": "高级鱼房投注金额",
            "filter": [
                {"term": {"bet_order_sub_type.keyword": "fish"}},
                {"terms": {"bet_order_third_type.keyword": ["fish_3", "fish_4", "3", "4"]}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "channel_tax_amount",
            "target_index": ["dark3", 'dark2', 'dark4'],
            "value_name": "渠道税收",
            "value_expr": "tax_amount + high_level_fish_bet_amount * 0.015",
            "adjust_amount": True,
            "description": "以渠道为维度之总和，包含捕鱼等游戏的隐藏税收"
        },
        {
            "value_id": "lose_award_amount",
            "target_value": "bet_order_award_amount",
            "value_name": "负扣金额",
            "filter": [
                {"terms": {"bet_order_sub_type.keyword": NO_BET_AMOUNT_GAME_TYPE}},
                {"range": {"bet_order_award_amount": {"lt": 0}}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "invalid_bet_amount",
            "target_value": "bet_order_bet_amount",
            "value_name": "无效投注",
            "filter": [
                {"terms": {"bet_order_sub_type.keyword": NO_WIN_AMOUNT_GAME_TYPE}},
                {"range": {"bet_order_award_amount": {"gt": 0}}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "valid_bet_amount",
            "value_name": "有效投注",
            "value_expr": "_bet_amount - invalid_bet_amount + (lose_award_amount * -1)",
            "adjust_amount": True,
            "description": "斗地主/血拼牛牛：”赢家”投注金额总和，\n诈金花/梭哈：”输家”投注金额总和，\n其馀游戏等同投注金额"
        },
        {
            "value_id": "bet_profit_amount",
            "target_value": ["bet_order_bet_amount", "-", "bet_order_award_amount"],
            "value_name": "玩法营收",
            "filter": [
                {'terms': {'_event_id': ['bet_order']}},
            ],
            "value_type": "float",
            "adjust_amount": True,
            "description": "投注金额 - 中奖金额"
        },
        {
            "value_id": "profit_rate",
            "value_name": "抽水率",
            "value_expr": "bet_profit_amount / valid_bet_amount",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100)),
            "description": "玩法营收 / 有效投注金额"
        },
        {
            "value_id": "recharge_user",
            "target_value": "_user_id",
            "value_name": "充值账号",
            "filter": [
                {'terms': {'_event_id': ['recharge']}}
            ],
            "operation": "cardinality",
            "value_type": "long",
            "description": "充值用户的数量"
        },
        {
            "value_id": "first_recharge_user",
            "target_value": "_user_id",
            "value_name": "首充账号",
            "filter": [
                {"term": {"_event_id": "recharge"}},
                {"terms": {"_user_id": "${first_recharge_user_pool}"}}
            ],
            "operation": "cardinality",
            "description": "当天有第一次充值行为的用户数量"
        },
        {
            "value_id": "recharge_rate",
            "value_name": "注充率",
            "value_expr": "recharge_user/real_active_user",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100)),
            "description": "充值人数 / 活跃账号"
        },
        {
            "value_id": "recharge_apply_count",
            "target_value": "recharge_apply_price",
            "value_name": "付费申请次数",
            "filter": [
                {'terms': {'_event_id': ['recharge_apply']}}
            ],
            "operation": "value_count",
            "value_type": "long",
            "hide_result": True
        },
        {
            "value_id": "recharge_count",
            "target_value": "recharge_price",
            "value_name": "充值次数",
            "filter": [
                {'term': {'_event_id': 'recharge'}}
            ],
            "operation": "value_count",
            "value_type": "long",
            "hide_result": True,
        },
        {
            "value_id": "recharge_success_rate",
            "value_name": "充值成功率",
            "value_expr": "recharge_count/recharge_apply_count",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100)),
        },
        {
            "value_id": "recharge_price",
            "target_value": "recharge_price",
            "value_name": "充值金额",
            "filter": [
                {'terms': {'_event_id': ['recharge']}}
            ],
            "value_type": "float",
            "adjust_amount": True,
        },
        {
            "value_id": "first_recharge_",
            "target_value": "recharge_price",
            "value_name": "首充充值",
            "filter": [
                {"term": {"_event_id": "recharge"}},
                {"terms": {"_user_id": "${first_recharge_user_pool}"}}
            ],
            "value_type": "float",
            "adjust_amount": True,
            "description": "当天有以第一次充值行为的用户，当天累积充值的金额"
        },
        {
            "value_id": "recharge_user_arpu",
            "value_name": "ARPPU",
            "value_expr": "recharge_price/recharge_user",
            "adjust_amount": True,
            "description": "所有付费账号人均付费金额，付费金额 / 付费账号"
        },
        {
            "value_id": "real_active_user_arpu",
            "value_name": "ARPU",
            "value_expr": "recharge_price/real_active_user",
            "adjust_amount": True,
            "description": "所有活跃账号人均付费金额，付费金额 / 活跃账号"
        },
        {
            "value_id": "withdraw_user",
            "target_value": "_user_id",
            "value_name": "提现账号",
            "filter": [
                {'term': {'_event_id': 'withdraw'}}
            ],
            "operation": "cardinality",
            "value_type": "long"
        },
        {
            "value_id": "withdraw_price",
            "target_value": "withdraw_real_price",
            "value_name": "提现金额",
            "filter": [
                {'term': {'_event_id': 'withdraw'}}
            ],
            "value_type": "float",
            "adjust_amount": True
        },
        {
            "value_id": "profit_amount",
            "value_name": "财务营收",
            "value_expr": "recharge_price - withdraw_price",
            "adjust_amount": True,
            "description": "充值金额 - 提现金额"
        },
        {
            "value_id": "profit_amount_rate",
            "value_name": "利润率",
            "value_expr": "profit_amount / recharge_price",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100)),
            "description": "财务营收 / 充值金额"
        },
    ]
}
